﻿using System;
using FluentFTP.Model;
using FluentFTP.ViewModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace FluentFTPTest
{
    [TestClass]
    public class MainViewModelTest
    {
        private const string rootFtpAddress = "ftp://127.0.0.1";
        private const string defaultSavePath = "C:\\t";
        private const string rootFileSample = "test.txt";
        private const string rootFolderSample = "Test";

        private FtpServerInfo serverInfo = new FtpServerInfo { Adress = rootFtpAddress, UserName = "anonymous", Password = string.Empty };

        //[TestMethod]
        //public void ConnectCommandTest()
        //{
        //    MainViewModel viewModel = getMainViewModelInstance();

        //    viewModel.ConnectCommand.Execute(serverInfo);

        //    Assert.IsTrue(viewModel.Folders != null);
        //    Assert.IsTrue(viewModel.Folders.Any(x => x.Name == rootFolderSample) && viewModel.Folders.First(x => x.Name == rootFolderSample).Type == FtpItemType.Folder);
        //    Assert.IsTrue(viewModel.Folders.Any(x => x.Name == rootFileSample) && viewModel.Folders.First(x => x.Name == rootFileSample).Type == FtpItemType.File);

        //    viewModel.CurrentPath = rootFtpAddress + "/" + rootFolderSample;
        //    Assert.IsTrue(viewModel.Folders.Any(x => x.Name == "happyLaptopGirl.jpg") && viewModel.Folders.First(x => x.Name == "happyLaptopGirl.jpg").Type == FtpItemType.File);
        //}

        

        [TestMethod]
        public void DefaultActionCommandFolderTest()
        {
            var viewModel = getMainViewModelInstance();

            viewModel.ConnectDialogCommand.Execute();
            viewModel.SelectedItem = viewModel.Folders.First(x => x.Name == rootFolderSample);
            viewModel.DefaultActionCommand.Execute();

            Assert.IsTrue(viewModel.CurrentPath.Contains(rootFtpAddress + "//" + rootFolderSample));
        }

        [TestMethod]
        public void DefaultActionCommandFileTest()
        {
            var viewModel = getMainViewModelInstance();

            viewModel.ConnectCommand.Execute(serverInfo);
            viewModel.SelectedItem = viewModel.Folders.First(x => x.Name == rootFileSample);
            viewModel.DefaultActionCommand.Execute();

            string filePath = defaultSavePath + "\\" + rootFileSample;
            Assert.IsTrue(File.Exists(filePath));
            Debug.WriteLine("File Length: " + (new FileInfo(filePath)).Length);
            Assert.IsTrue((new FileInfo(filePath)).Length == 1788);
        }

        [TestMethod]
        public void DownloadCommandTest()
        {
            var viewModel = getMainViewModelInstance();

            viewModel.ConnectCommand.Execute(serverInfo);
            

        }

        private MainViewModel getMainViewModelInstance()
        {
            MainViewModel viewModel = new MainViewModel();

            viewModel.Adress = rootFtpAddress;
            viewModel.UserName = "anonymous";
            viewModel.Password = "password";

            return viewModel;
        }
    }
}
